package com.itheima.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domian.Category;
import com.itheima.domian.Setmeal;
import com.itheima.dto.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService categoryService;

    //分页查询
    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        setmealService.page(page1, Wrappers.<Setmeal>lambdaQuery().eq(StringUtils.isNotBlank(name), Setmeal::getName, name));

        Page<SetmealDto> page2 = new Page<>(page, pageSize);
        BeanUtils.copyProperties(page1, page2, "records");

        List<SetmealDto> collect = page1.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getOne(Wrappers.<Category>lambdaQuery().eq(Category::getId, setmeal.getCategoryId()));
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        page2.setRecords(collect);
        return R.success(page2);
    }

    //新增
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //自定义方法
        setmealService.add(setmealDto);
        return R.success("添加成功");
    }

    //批量/起售
    @PostMapping("status/1")
    public R<String> status(Long[] ids) {
        //修改数据
        setmealService.updateStatus(ids);
        return R.success("起售成功");
    }


    //批量/停售
    @CacheEvict(cacheNames = "cachenSetmeal", allEntries = true)
    @PostMapping("status/0")
    public R<String> status1(Long[] ids) {
        setmealService.updateStop(ids);
        return R.success("停售成功");
    }

    //删除/批量删除
    @DeleteMapping
    @CacheEvict(cacheNames = "cachenSetmeal", allEntries = true)
    public R<String> delete(Long[] ids) {
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    //修改数据插叙回显
    @GetMapping("/{id}")
    public R<SetmealDto> queryById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.querySetmealById(id);
        return R.success(setmealDto);
    }

    //修改数据
    @PutMapping
    @CacheEvict(cacheNames = "cachenSetmeal", key = "#setmealDto.categoryId")
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改成功");
    }

    //查询手机端套餐 对应的菜品数据
    @GetMapping("list")
    @Cacheable(cacheNames = "cachenSetmeal", key = "#setmeal.categoryId", unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        List<Setmeal> list = setmealService.list(Wrappers.lambdaQuery(Setmeal.class).
                eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(Setmeal::getStatus, "1"));
        return R.success(list);
    }
}
